package org.liwuyi.demo.controller;

import jakarta.annotation.Resource;
import org.liwuyi.demo.entity.Personnel;
import org.liwuyi.demo.service.PersonnelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 人员信息Controller
 */
@RestController
@RequestMapping("/personnel")
public class PersonnelController {

    @Resource
    private PersonnelService personnelService;

    /**
     * 批量添加人员信息
     * @param personnelList 人员信息列表
     * @return 结果
     */
    @PostMapping("/batchAdd")
    public String batchAddPersonnel(@RequestBody List<Personnel> personnelList) {
        System.out.println("进入批量添加人员信息方法");
        System.out.println(personnelList);
        try {
            if (personnelList == null || personnelList.isEmpty()) {
                return "人员列表不能为空";
            }
            int result = personnelService.batchAddPersonnel(personnelList);
            return "成功添加 " + result + " 条人员信息";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败：" + e.getMessage();
        }
    }
    
    /**
     * 获取数据变更信息
     * @return 包含新增和删除ID的Map
     */
    @GetMapping("/dataChanges")
    public Map<String, Object> getDataChanges() {
        Map<String, Object> result = new HashMap<>();
        
        CopyOnWriteArrayList<Long> addedIds = personnelService.getAddedIds();
        CopyOnWriteArrayList<Long> removedIds = personnelService.getRemovedIds();
        
        result.put("addedIds", addedIds);
        result.put("removedIds", removedIds);
        result.put("message", "数据变更查询成功");
        
        return result;
    }
}